package com.changgou.goods.service.impl;

import com.changgou.goods.dao.AlbumMapper;
import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: wzw
 * @Date: 2020/12/8 20:06
 * @version: 1.8
 */
@Service
public class AlbumServiceImpl implements AlbumService {

    //注入AlbumMapping
    @Autowired(required = false)
    private AlbumMapper albumMapper;

    /**
     * @param album 对象
     * @param page  第几页
     * @param size  每页多少行
     * @return java.util.List<com.changgou.goods.pojo.Album>
     * @author wzw
     * Album多条件分页查询
     * @Date 20:04 2020/12/8
     **/
    @Override
    public PageInfo<Album> findPage(Album album, Integer page, Integer size) {
        //设置分页查询条件
        PageHelper.startPage(page, size);
        //设置查询条件
        Example example = createExample(album);
        //实现功能:按条件查询
        List list = albumMapper.selectByExample(example);
        //实现功能:new 分页对象(结果集)并且返回
        return new PageInfo<Album>(list);
    }

    /**
     * Album分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Album> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Album>(albumMapper.selectAll());
    }

    /**
     * Album条件查询
     * @param album
     * @return
     */
    @Override
    public List<Album> findList(Album album){
        //构建查询条件
        Example example = createExample(album);
        //根据构建的条件查询数据
        return albumMapper.selectByExample(example);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        albumMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Album
     * @param album
     */
    @Override
    public void update(Album album){
        albumMapper.updateByPrimaryKey(album);
    }

    /**
     * 增加Album
     * @param album
     */
    @Override
    public void add(Album album){
        albumMapper.insert(album);
    }

    /**
     * 根据ID查询Album
     * @param id
     * @return
     */
    @Override
    public Album findById(Long id){
        return  albumMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Album全部数据
     * @return
     */
    @Override
    public List<Album> findAll() {
        return albumMapper.selectAll();
    }

    /**
     * @author wzw
     * 设置要查询的条件
     * @Date 20:25 2020/12/8
     * @param album 对象
     * @return tk.mybatis.mapper.entity.Example
    **/
    private Example createExample(Album album) {
        //设置的例子
        Example example = new Example(Album.class);
        //添加条件(给参数之间加and)
        Example.Criteria criteria = example.createCriteria();
        //判断对象是否为空
        if (album != null) {
            //判断编号是否为空
            if (!StringUtils.isEmpty(album.getId())){
                //添加到例子中
                criteria.andEqualTo("id" ,album.getId());
            }
            //判断标题是否为空
            if (!StringUtils.isEmpty(album.getTitle())){
                //添加到例子中==>这里采用模糊查询
                criteria.andLike("title", "%" + album.getTitle() + "%");
            }
            //判断图片是否为空
            if (!StringUtils.isEmpty(album.getImage())){
                //添加到例子中
                criteria.andEqualTo("image",album.getImage());
            }
            // 图片列表
            if(!StringUtils.isEmpty(album.getImageItems())){
                criteria.andEqualTo("imageItems",album.getImageItems());
            }
        }
        return example;
    }
}
